/* Tables of covariate predictions for phases I-III and VI, V
*/

clear
set more off

*Set working directory to rd graph folder:
cd "${main}"

cap log close
log using "${logs}/tables_covariate_prediction.log", replace
								

/*=================================
Predicting correct classification based on
player characteristics
=======================================*/

cap program drop var_prep
program define var_prep


****Balance of covariates***
gen female = playergender == 1
* College stats: College level or Graduate level
gen grad_stats = playerstats_knowledge == 4
* College education: bachelors, masters, PhD
gen college_ed = playereducation == 5 | playereducation == 6 | playereducation == 7
gen age18_22 = playerage == 1
gen age23_49 = playerage == 2 | playerage == 3
gen age50_old = playerage == 4 | playerage == 5
destring playerprofession, replace
gen student = playerprofession == 1 | playerprofession == 2

cap gen freshplayer = (repeat_participant == 2)

*Variable labels		
label variable female "Female"		
label variable college_ed "Completed college"
label variable age18_22 "Age 18 to 22"
label variable age23_49 "Age 23 to 49"
label variable age50_old "Age 50 or older"
label variable grad_stats "Graduate stats knowledge"
label variable student "Current student"
label variable attention "Passed attention check"
cap label variable freshplayer "First time player"

end 

cap file close t
capture erase "${output}/covariate_prediction.tex"
	file open t using "${output}/covariate_prediction.tex", write replace

		file write t ///
		"\resizebox*{4.0in}{!}{%"_n ///
		"\begin{tabular*}{1.0\hsize}{@{\hskip\tabcolsep\extracolsep\fill}l c c c c } "_n ///
		"\toprule "_n ///
		" &\multicolumn{4}{c}{} \\"_n ///
		" &    Phase 1 &  Phase 2  & Phase 3 & Phase 4  \\"_n ///
		"\cline{2-5} \\" _n ///
		"\midrule" _n

				
	/*==============================================================================
Phase I Table
-----------------------------------*/
use "${dat}/Pilot 1.0/survey_merged.dta", clear
cap drop _merge

var_prep
	
reg playercorrect attention female college_ed   age23_49 age50_old grad_stats, vce(cluster pid)

foreach var of varlist female college_ed   age23_49 age50_old grad_stats attention {
local b_`var'_1 = string(_b[`var'], "%10.3fc")
local se_`var'_1 = string(_se[`var'], "%10.3fc")
}

test attention female college_ed   age23_49 age50_old grad_stats
local pval_1 = string(r(p), "%10.3fc")
	
	/*==============================================================================
Phase II Table
-----------------------------------*/
	
*----
use "${dat}/Phase II/survey_merged_PhaseII.dta", clear
****************************************************


var_prep
		
reg playercorrect attention female college_ed   age23_49 age50_old grad_stats freshplayer, vce(cluster pid)
foreach var of varlist female college_ed   age23_49 age50_old grad_stats attention freshplayer {
local b_`var'_2 = string(_b[`var'], "%10.3fc")
local se_`var'_2 = string(_se[`var'], "%10.3fc")
}

test attention female college_ed   age23_49 age50_old grad_stats freshplayer
local pval_2 = string(r(p), "%10.3fc")
	
	/*==============================================================================
Phase III
-------------------------------*/

*----
use "${dat}/Phase III/survey_merged_PhaseIII.dta", clear

var_prep
			
reg playercorrect attention female college_ed   age23_49 age50_old grad_stats freshplayer, vce(cluster pid)
foreach var of varlist female college_ed   age23_49 age50_old grad_stats attention freshplayer {
local b_`var'_3 = string(_b[`var'], "%10.3fc")
local se_`var'_3 = string(_se[`var'], "%10.3fc")
}

test attention female college_ed   age23_49 age50_old grad_stats freshplayer
local pval_3 = string(r(p), "%10.3fc")

	/*==============================================================================
Phase VI Table
-----------------------------------*/
	
*----
use "${dat}/Phase VI/survey_merged_PhaseVI.dta", clear
****************************************************


var_prep
		
reg playercorrect attention female college_ed   age23_49 age50_old grad_stats freshplayer, vce(cluster pid)
foreach var of varlist female college_ed   age23_49 age50_old grad_stats attention freshplayer {
local b_`var'_6 = string(_b[`var'], "%10.3fc")
local se_`var'_6 = string(_se[`var'], "%10.3fc")
}

test attention female college_ed   age23_49 age50_old grad_stats freshplayer
local pval_6 = string(r(p), "%10.3fc")

***
	
local b_freshplayer_1 = "NA"
local se_freshplayer_1 = "NA"	

foreach var of varlist female college_ed   age23_49 age50_old grad_stats attention freshplayer {

file write t "`: variable label `var''   & `b_`var'_1' & `b_`var'_2' & `b_`var'_3' & `b_`var'_6' \\"_n
file write t "   & (`se_`var'_1') & (`se_`var'_2') & (`se_`var'_3') & (`se_`var'_6')  \\"_n

}

file write t  "\cline{1-5} \\" _n 
file write t "Joint test p-value & `pval_1' & `pval_2' & `pval_3' & `pval_6' \\"_n  
	
	file write t "\bottomrule \\" _n ///
	"\end{tabular*} "_n ///
	"}"_n
	
	file close t

	
log close
